package cn.kgc.spring04.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/4/28
 * @Description:
 */
public class CGlibProxy {

    private Object target;

    public CGlibProxy(Object object){
        this.target =object;
    }


    public Object getProxy(){

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        InvocationHandler invocationHandler =    new InvocationHandler(){
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

                System.out.println("------------log--------------");
                Object invoke = method.invoke(target, objects);
                System.out.println("------------log--------------");
                return invoke;
            }
        };
        enhancer.setCallback(invocationHandler);
        Object o = enhancer.create();
        return o;
    }

}
